<style lang="less">
    @import "./main.less";
</style>
<template>
    <div class="main" :class="{'main-hide-text': shrink}">
        <div class="sidebar-menu-con" :style="{width: shrink?'60px':'200px', overflow: shrink ? 'visible' : 'auto'}">
            <shrinkable-menu
                :shrink="shrink"
                :menu-list="menuList">
                <div slot="top" class="logo-con">
                    <!--<img v-show="!shrink"  src="../images/logo.jpg" key="max-logo" />
                    <img v-show="shrink" src="../images/logo-min.jpg" key="min-logo" />-->
                    <span v-show="!shrink" :style="{color: '#ffffff', fontSize: '29px'}">超V购</span>
                    <span v-show="shrink" :style="{color: '#ffffff', fontSize: '24px'}">V</span>
                </div>
            </shrinkable-menu>
        </div>
        <div class="main-header-con" :style="{paddingLeft: shrink?'60px':'200px'}">
            <div class="main-header">
                <div class="navicon-con">
                    <Button :style="{transform: 'rotateZ(' + (this.shrink ? '-90' : '0') + 'deg)'}" type="text" @click="toggleClick">
                        <Icon type="navicon" size="32"></Icon>
                    </Button>
                </div>
                <div class="header-avator-con">
                    <full-screen v-model="isFullScreen" @on-change="fullscreenChange" ></full-screen>

                    <div class="user-dropdown-menu-con" :style="{width: '127px'}">
                        <Row type="flex" justify="end" align="middle" class="user-dropdown-innercon">
                            <Dropdown transfer trigger="click" @on-click="handleClickUserDropdown">
                                <a href="javascript:void(0)">
                                    <span class="main-user-name" :style="{width: '60px'}">{{ userName }}</span>
                                    <Icon type="arrow-down-b"></Icon>
                                </a>
                                <DropdownMenu slot="list">
                                    <DropdownItem name="editpwd"><Icon type="log-out"></Icon> 修改密码</DropdownItem>
                                    <DropdownItem name="loginout" divided><Icon type="log-out"></Icon> 退出登录</DropdownItem>
                                </DropdownMenu>
                            </Dropdown>
                            <Avatar icon="person" style="background: #619fe7;margin-left:10px;"></Avatar>
                        </Row>
                    </div>
                </div>
            </div>
        </div>
        <div class="single-page-con" :style="{left: shrink?'60px':'200px'}">
            <div class="single-page">
                <router-view></router-view>
            </div>
        </div>
        <Modal v-model="modalEdit" width="660">
            <p slot="header" style="text-align:center">
                <Icon type="information-circled"></Icon>
                <span>修改密码</span>
            </p>
            <div style="text-align:center">
                <Form ref="formCustom" :model="formCustom" :rules="ruleCustom" :label-width="80">
                    <FormItem label="原始密码" prop="opwd">
                        <Input type="password" v-model="formCustom.opwd"></Input>
                    </FormItem>
                    <FormItem label="新设密码" prop="passwd">
                        <Input type="password" v-model="formCustom.passwd"></Input>
                    </FormItem>
                    <FormItem label="再次确认" prop="passwdCheck">
                        <Input type="password" v-model="formCustom.passwdCheck"></Input>
                    </FormItem>
                    <FormItem>
                        <Button type="primary" @click="handleSubmit('formCustom')">确认修改</Button>
                        <Button type="ghost" @click="handleReset('formCustom')" style="margin-left: 8px">重新填写</Button>
                    </FormItem>
                </Form>
            </div>
            <div slot="footer" style="text-align: center;">
                <!--<Button type="success" size="large" long :loading="modal_loading" @click="savepwd">修改</Button>-->
            </div>
        </Modal>
    </div>
</template>
<script>
import Util from '../libs/util';
import Cookies from 'js-cookie';
import shrinkableMenu from './main-components/shrinkable-menu/shrinkable-menu.vue';
import fullScreen from './main-components/fullscreen.vue';

export default {
    components: {
        shrinkableMenu,
        fullScreen
    },
    data () {
        const validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('输入新密码'));
            } else {
                if (this.formCustom.passwdCheck !== '') {
                    // 对第二个密码框单独验证
                    this.$refs.formCustom.validateField('passwdCheck');
                }else {
                    const regex = /^(?!([a-zA-Z]+|\d+)$)[a-zA-Z\d]{6,16}$/;
                    if(!regex.test(value)) {
                        callback(new Error('登录密码必须6~16位，且必须数字与字母组合'));
                    }
                }
                callback();
            }
        };
        const validatePassCheck = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('确认新设密码'));
            } else if (value !== this.formCustom.passwd) {
                callback(new Error('两次密码输入补一致'));
            } else {
                callback();
            }
        };
        const validateAge = (rule, value, callback) => {
            if (!value) {
                callback(new Error('输入原始密码'));
            }
            callback();
        };
        return {
            shrink: false,
            userName: '',
            token: '',
            isFullScreen: false,
            modalEdit: false,
            modal_loading: false,

            formCustom: {
                passwd: '',
                passwdCheck: '',
                opwd: ''
            },
            ruleCustom: {
                passwd: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                passwdCheck: [
                    { validator: validatePassCheck, trigger: 'blur' }
                ],
                opwd: [
                    { validator: validateAge, trigger: 'blur' }
                ]
            }
        };
    },
    computed: {
        menuList () {
            return this.$store.state.app.menuList;
        },
        menuTheme () {
            return this.$store.state.app.menuTheme;
        }
    },
    methods: {
        init () {
            this.userName = Cookies.get('user');
            this.token = Cookies.get('token');
            if (!this.token) {
                this.$router.push({
                    name: 'login'
                });
            }
        },
        toggleClick () {
            this.shrink = !this.shrink;
        },
        handleClickUserDropdown (name) {
            if(name == 'loginout') {
                this.logout();
            }
            if(name == 'editpwd') {
                this.modalEdit = true;
            }
        },
        fullscreenChange (isFullScreen) {
            // console.log(isFullScreen);
        },
        logout () {
            this.userName = Cookies.remove('user');
            this.token = Cookies.remove('token');
            this.$router.push({
                name: 'login'
            });
        },
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    Util.ajax.post(`/adminapi/user/editpwd`, this.formCustom).then(response => {
                        if (response.data.code == 0) {
                            this.$Message.success('修改成功');
                            this.modalEdit = false;
                            this.$refs[name].resetFields();
                        }else {
                            this.$Message.error(response.data.msg);
                        }
                    });
                }
            });
        },
        handleReset (name) {
            this.$refs[name].resetFields();
        }
    },
    mounted () {
        this.init();
    }
};
</script>
